package cn.wolfcode.util;

import cn.wolfcode.domain.Employee;
import org.springframework.web.context.request.RequestContextHolder;
import org.springframework.web.context.request.ServletRequestAttributes;

import javax.servlet.http.HttpSession;
import java.util.List;

public abstract class UserContext {

    public static final String EMPLOYEE_IN_SESSION = "EMPLOYEE_IN_SESSION";
    public static final String EXPRESSIONS_IN_SESSION = "EXPRESSIONS_IN_SESSION";

    public static Employee getEmployee(){
        HttpSession session = getSession();
        return  (Employee) session.getAttribute(EMPLOYEE_IN_SESSION);
    }

    private static HttpSession getSession() {
        ServletRequestAttributes requestAttributes = (ServletRequestAttributes) RequestContextHolder.getRequestAttributes();
        return requestAttributes.getRequest().getSession();
    }

    public static void setEmployee(Employee employee){
        HttpSession session = getSession();
        session.setAttribute(EMPLOYEE_IN_SESSION,employee);

    }
    public static void removeEmployee(){
        HttpSession session = getSession();
        session.removeAttribute(EMPLOYEE_IN_SESSION);

    }

    public static void setExpressions(List<String> expressions) {
        HttpSession session = getSession();
        session.setAttribute(EXPRESSIONS_IN_SESSION,expressions);
    }

    public static List<String> getExpressions() {
        HttpSession session = getSession();
       return (List<String>) session.getAttribute(EXPRESSIONS_IN_SESSION);

    }
}
